import java.awt.EventQueue;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import com.lofter.heras.ui.HashFile;

/**
 * runner class
 * 
 * @author heras
 * 
 */
public class Runner {

	public Runner() {
	}

	/**
	 * run method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		HashFile file = new HashFile();
		// update look and feel
		updateLookAndFeel(file);

		// print argument list
		if (args.length > 0) {
			System.out.println("argument list:");
			for (int i = 0; i < args.length; i++) {
				System.out.println("argument[" + i + "]=" + args[i]);
			}
		}
		// run in command mode
		if (args.length == 2) {
			file.commandMode = true;
			file.run(args[0], args[1]);
			file.commandMode = false;
		} else if (args.length > 0) {
			System.out
					.println("usage: java -jar <jar file> [<hash file> <md5|sha1>]");
		}
	}

	/**
	 * update look and feel
	 * 
	 * @param f
	 */
	private static void updateLookAndFeel(HashFile f) {
		if (f != null) {
			final HashFile file = f;
			LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
			String classname = "";
			for (LookAndFeelInfo look : looks) {
				String name = look.getName();
				if ("Windows".equals(name)) {
					classname = look.getClassName();
					break;
				}
			}

			if (classname.length() > 0) {
				try {
					UIManager.setLookAndFeel(classname);
					SwingUtilities.updateComponentTreeUI(file);
					SwingUtilities.updateComponentTreeUI(file.getFileChoose());
				} catch (ClassNotFoundException | InstantiationException
						| IllegalAccessException
						| UnsupportedLookAndFeelException e) {
				}
			}

			EventQueue.invokeLater(new Runnable() {
				@Override
				public void run() {
					file.setVisible(true);
				}
			});
		}
	}
}
